package com.xxxt.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;





public class FileUploadUtil {

	//校验文件格式
	public static boolean CheckFile(String filename) {
		String [] name= {"doc","DOC","docx","DOCX"};
		for (String string : name) {
			if(filename.contains(string)) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 
	 * @description：获取文件后缀名,带点的
	 * @param fileFullName
	 * @return：
	 */
	public static String getFileNameEnds(String fileFullName) {
		int lastIdx = fileFullName.lastIndexOf(".");
		if (lastIdx < 1) {
			return "";
		} else {
			return fileFullName.substring(lastIdx);
		}
	}
	/**
	 * 
	 * @description：通过文件全称来判断
	 * @param filename
	 * @return：
	 */
	public static boolean  CheckImages(String filename) {
	String [] name= {"jpg","JPG","PNG","png","jpeg","JPEG"};
		for (String string : name) {
			if(filename.contains(string)){
				return true;
			}	
		}
			return false;
	}
	/**
	 * 
	 * @description：根据文件内容判断是否为图片
	 * @param file
	 * @throws IOException 
	 * @return：
	 */
	public static boolean  CheckImages(MultipartFile file) throws IOException {
			BufferedImage read = ImageIO.read(file.getInputStream());
			if(read!=null) {
				return true;
			}
			return false;
		}
	public static boolean uploadProject(String filename) {
	String [] name= {"rar","RAR","zip","ZIP"};
		for (String string : name) {
			if(filename.contains(string)){
				return true;
			}	
		}
				return false;
	}
	public static boolean uploadExcel(String filename) {
		String [] name= {"xlsx","XLSX","XLS","xls"};
		for (String string : name) {
			if(filename.contains(string)){
				return true;
			}	
		}
		return false;
	}
	/**
	 * 上传文件处理,spring专属
	 * @param file
	 * @param request
	 * @param response
	 * @param asName 文件别名
	 * @param rootPath 子目录,开头已有"/"
	 * @return 上传后的文件
	 */
	public static File upFilebySpring(MultipartFile file,HttpServletRequest request,HttpServletResponse response,
			String asName,String rootPath) {

		FileUploadUtil.createDirectory(request,response,rootPath); // 创建目录

		String fileFullName = file.getOriginalFilename(); // 获取文件名全称
		
		String path = request.getServletContext().getRealPath("/") + "/" + rootPath + "/" + asName + FileUploadUtil.getFileNameEnds(fileFullName); // 保存文件的路径

		try {
			file.transferTo(new File(path));
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
			return null;
		}
		return new File(path);
	}/**
	 * 在request.getServletContext().getRealPath目录下创建子目录
	 * @param request
	 * @param response
	 * @param rootPath 子目录,开头已有"/"
	 * @return
	 */
	public static void createDirectory(HttpServletRequest request,HttpServletResponse response,String rootPath){
		System.setProperty("sun.jnu.encoding", "utf-8");//创建目录,解决中文乱码
		File dir = new File(request.getServletContext().getRealPath("/")+"/"+rootPath);
		dir.mkdirs();
	}
	/**
	 * 获取不包括后缀的文件名
	 * @param fileFullName
	 * @return
	 */
	public static String getFileNameExcludingEnds(String fileFullName) {
		int lastIdx = fileFullName.lastIndexOf(".");
		if (lastIdx < 1) {
			return fileFullName;
		} else {
			return fileFullName.substring(0, lastIdx);
		}
	}
	/**
	 * 判断文件是否存在
	 * @param path
	 * @return
	 */
	public static boolean isNotFile(HttpServletRequest request,String path) {
		File f = new File(request.getServletContext().getRealPath("/") + "/" + path);
        if (f.exists()) {
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 
	 * @author: wsc
	 * @date:2020年6月17日 上午8:48:52
	 * @description：获取工程的绝对路径
	 * @return：
	 */
	public static String getRealPath() {
		String path = FileUploadUtil.class.getClassLoader().getResource("").getPath();
		return path;
	}
	/*
	 * 根据浏览器的不同进行编码设置，返回编码后的文件名
	 */
	public static String getFilename(HttpServletRequest request,String filename) throws Exception { 
	    // IE不同版本User-Agent中出现的关键词
	    String[] IEBrowserKeyWords = {"MSIE", "Trident", "Edge"};  
	    // 获取请求头代理信息
	    String userAgent = request.getHeader("User-Agent");  
	    for (String keyWord : IEBrowserKeyWords) { 
	         if (userAgent.contains(keyWord)) { 
	              //IE内核浏览器，统一为UTF-8编码显示
	              return URLEncoder.encode(filename, "UTF-8");
	         }
	    }  
	    //火狐等其它浏览器
	    return new String(filename.getBytes("UTF-8"), "ISO-8859-1");  
	} 
	
}
